package resources

import (
	"api01/models"
)

type NGroup struct {
	Id int
	Name string
	ChildList []NGroup
}
/*构建group数据格式*/
func BuildGroupDataStruct(data []models.Group) (ret []NGroup) {
	if len(data) <=0 {
		return
	}
	aGroupsList,dGroupsList := []models.Group{},[]NGroup{}
	for _,v := range data{
		group := models.Group{
			Id:        v.Id,
			Name:      v.Name,
			Pid:      v.Pid,
		}
		aGroupsList = append(aGroupsList,group)
		if v.Pid == 0 {
			dGroupsList = append(dGroupsList,NGroup{
				Id:        group.Id,
				Name:      group.Name,
				ChildList: nil,
			})
		}
	}
	for _,v := range dGroupsList{
		ret = append(ret,recursionBuild(aGroupsList, v))
	}
	return
}

/*递归获取自己的子集*/
func recursionBuild(odata []models.Group,pdata NGroup) NGroup{
	for _,v := range odata {
		if pdata.Id == v.Pid {
			build := recursionBuild(odata, NGroup{
				Id:        v.Id,
				Name:      v.Name,
				ChildList: nil,
			})
			pdata.ChildList = append(pdata.ChildList, build)
		}
	}
	return pdata
}